package com.telrob.bytevm.instruct;

import java.util.List;

import com.telrob.bytevm.compile.Instruct;
import com.telrob.bytevm.compile.ReplaceCode;
import com.telrob.bytevm.utils.Define;
import com.telrob.bytevm.utils.ToolUtils;
import com.telrob.bytevm.vm.Register;

public class DIV implements Instruct {
	
	public boolean isPseudo(String opt) {
		return false;
	}
	
	@Override
	public boolean isComplie(String opt) {
		if(opt.toLowerCase().startsWith("div")) {
			return true;
		}
		return false;
	}

	@Override
	public byte[] complie(String line,String[] param,int curretIndex,List<ReplaceCode>labelReplace) {
		byte[] data=null;
		String opt=param[0];
		if("divrr".equals(opt.toLowerCase())) {
			data=new byte[3];
			data[0]=Define.DIVRR;
			int number1=0;
			int number2=0;
			String r1=param[1].toLowerCase();
			if("ax".equals(r1)) {
				number1=Define.AX;
			}else if("bx".equals(r1)) {
				number1=Define.BX;
			}else if("cx".equals(r1)) {
				number1=Define.CX;
			}else if("dx".equals(r1)) {
				number1=Define.DX;
			}else if("ex".equals(r1)) {
				number1=Define.EX;
			}else if("ss".equals(r1)) {
				number1=Define.SS;
			}else if("si".equals(r1)) {
				number1=Define.SI;
			}else if("sp".equals(r1)) {
				number1=Define.SP;
			}else if("bp".equals(r1)) {
				number1=Define.BP;
			}else if("di".equals(r1)) {
				number1=Define.DI;
			}
			
			String r2=param[2].toLowerCase();
			if("ax".equals(r2)) {
				number2=Define.AX;
			}else if("bx".equals(r2)) {
				number2=Define.BX;
			}else if("cx".equals(r2)) {
				number2=Define.CX;
			}else if("dx".equals(r2)) {
				number2=Define.DX;
			}else if("ex".equals(r2)) {
				number2=Define.EX;
			}else if("ss".equals(r2)) {
				number2=Define.SS;
			}else if("si".equals(r2)) {
				number2=Define.SI;
			}else if("sp".equals(r2)) {
				number2=Define.SP;
			}else if("bp".equals(r2)) {
				number2=Define.BP;
			}else if("di".equals(r2)) {
				number2=Define.DI;
			}
			data[1]=(byte)(number1&0xFF);
			data[2]=(byte)(number2&0xFF);
		}else if("divrn".equals(opt.toLowerCase())) {
			data=new byte[6];
			data[0]=Define.DIVRN;
			int number1=0;
			
			String r1=param[1].toLowerCase();
			if("ax".equals(r1)) {
				number1=Define.AX;
			}else if("bx".equals(r1)) {
				number1=Define.BX;
			}else if("cx".equals(r1)) {
				number1=Define.CX;
			}else if("dx".equals(r1)) {
				number1=Define.DX;
			}else if("ex".equals(r1)) {
				number1=Define.EX;
			}else if("ss".equals(r1)) {
				number1=Define.SS;
			}else if("si".equals(r1)) {
				number1=Define.SI;
			}else if("sp".equals(r1)) {
				number1=Define.SP;
			}else if("bp".equals(r1)) {
				number1=Define.BP;
			}else if("di".equals(r1)) {
				number1=Define.DI;
			}
			data[1]=(byte)(number1&0xFF);
			
			int number2=ToolUtils.parseNum(param[2]);
			byte[]temp=ToolUtils.int2bytes(number2);
			data[2]=temp[0];
			data[3]=temp[1];
			data[4]=temp[2];
			data[5]=temp[3];
			
		}
		return data;
	}

	public int isRun(byte opt) {
		if(opt==Define.DIVRR) {
			return 3;
		}else if(opt==Define.DIVRN) {
			return 6;
		}
		return -1;
	}
	public void parseRun(Register R,byte opt,byte[]data) {
		if(opt==Define.DIVRR) {
			byte rx=data[R.PC+1];
			byte rx2=data[R.PC+2];
			int number2=0;
			if(rx2==Define.AX) {
				number2=R.AX;
			}else if(rx2==Define.BX) {
				number2=R.BX;
			}else if(rx2==Define.CX) {
				number2=R.CX;
			}else if(rx2==Define.DX) {
				number2=R.DX;
			}else if(rx2==Define.EX) {
				number2=R.EX;
			}else if(rx2==Define.BP) {
				number2=R.BP;
			}else if(rx2==Define.SS) {
				number2=R.SS;
			}else if(rx2==Define.SP) {
				number2=R.SP;
			}
			
			if(rx==Define.AX) {
				R.AX=R.AX/number2;
			}else if(rx==Define.BX) {
				R.BX=R.BX/number2;
			}else if(rx==Define.CX) {
				R.CX=R.CX/number2;
			}else if(rx==Define.DX) {
				R.DX=R.DX/number2;
			}else if(rx==Define.EX) {
				R.EX=R.EX/number2;
			}else if(rx==Define.BP) {
				R.BP=R.BP/number2;
			}else if(rx==Define.SS) {
				R.SS=R.SS/number2;
			}else if(rx==Define.SP) {
				R.SP=R.SP/number2;
			}
			R.PC+=isRun(opt);
		}else if(opt==Define.DIVRN) {
			byte rx=data[R.PC+1];
			int number2=ToolUtils.getInt(data, R.PC+2);
			if(rx==Define.AX) {
				R.AX=R.AX/number2;
			}else if(rx==Define.BX) {
				R.BX=R.BX/number2;
			}else if(rx==Define.CX) {
				R.CX=R.CX/number2;
			}else if(rx==Define.DX) {
				R.DX=R.DX/number2;
			}else if(rx==Define.EX) {
				R.EX=R.EX/number2;
			}else if(rx==Define.BP) {
				R.BP=R.BP/number2;
			}else if(rx==Define.SS) {
				R.SS=R.SS/number2;
			}else if(rx==Define.SP) {
				R.SP=R.SP/number2;
			}
			R.PC+=isRun(opt);
		}
		
	}

	

}
